package com.jiusit.common;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.json.JSONObject;

import com.webbuilder.common.Var;
import com.webbuilder.utils.DbUtil;
import com.webbuilder.utils.FileUtil;
import com.webbuilder.utils.StringUtil;
import com.webbuilder.utils.SysUtil;
import com.webbuilder.utils.WebUtil;

/**
 * 用户附件上传类
 * @author chenpy
 *
 */
public class ActUpload {

	 @SuppressWarnings({ "rawtypes", "unused", "unchecked", "static-access" })
	 public static void loanActUpload(HttpServletRequest request,
				HttpServletResponse response) throws Exception{
		 Map res=new HashMap();
		 String fileName=(String) request.getAttribute("text1__name");
		 InputStream file_is=(InputStream) request.getAttribute("text1");
		 long file_size=(Long)request.getAttribute("text1__size");
		 //最大单张文件大小2M
		 long maxSize = 2*1024*1024;
		 String save_path=request.getRealPath(Var.get("api.zip.atc_path"))+"/"+
		     com.webbuilder.utils.DateUtil.formatDate(new java.util.Date(),"yyyyMMdd");
		 save_path=StringUtil.replace(save_path,"\\","/");
		 FileUtil.directoryCreate(save_path);
		 String upload_by=(String) request.getAttribute("user_id");
		 String upload_name=(String) request.getAttribute("real_name");
		 String imgType="gif,jpg,jpeg,png,bmp";
		 String fileExt=com.webbuilder.utils.FileUtil.extractFileExt(fileName);
		 String actTypp="01";
	     if(imgType.indexOf(fileExt)==-1){
	       actTypp="02";
	     }
		 //保存文件
		 String file_path=save_path+"/"+SysUtil.getId()+"."+fileExt;
		 FileUtil.saveStream(file_is,new java.io.File(file_path));
		 String act_url=file_path.substring(file_path.indexOf("/upload/")+1,file_path.length());
		 res.put("error",0);
		 //res.put("url",Var.get("weixin.domain")+"/"+act_url);
		 res.put("url",act_url);
		 WebUtil.response(response, new JSONObject(res).toString(),true);
		 file_is.close();
	 }
}
